;welcome! hopefully this scrap of code will show you how
;to set up your own major and derived modes
;as a treat, you'll see a minor mode definition at the bottom

;This adds the "atomic" group under "applications"
;you could access the customisation menu by doing
;M-x customize. If you don't know what the customize
;menu is, you don't know what you're missing. Type
;"M-x customize" right now...

;Atomic's parent is nil - because it's SUPER IMPORTANT
  (defgroup ATOMIC nil
    "Atomic major group."
    :version "0.00"
    :group 'applications);applications, since this is an application written in emacs

;we want to provide a hook, so people can add customisations to the mode easily.
;we can't add it inside the atom-major-mode function
;because the hook must be provided outside the function
  (defcustom atom-major-mode-hook nil
    "Hook for customizing 'atom-major-mode'."
    :type 'hook
    :group 'ATOMIC)
  

;our major mode - called, oddly enough, atom-major-mode
(defun atom-major-mode ()
  "Documentation string of the major mode. It is just a demonstration of a
mode, so we've called it atom-major-mode"
  (interactive)		   ;needed, so the function becomes available.
  (kill-all-local-variables)	    ;needed, so we can HAVE control...
  (setq major-mode 'atom-major-mode)
  (setq mode-name "Atom (maj)")	       ; This goes into the mode line.
  (make-local-variable 'atom-major-mode-greeting) ;a local variable
  (setq atom-major-mode-greeting "Hello, from the atomic major mode") ;do M-: atom-major-mode-greeting to see this variable

  (add-hook 'atom-major-mode-hook 'atom-major-mode-info)
  
  (setq atom-major-mode-map (make-sparse-keymap))
  (define-key atom-major-mode-map "\M-\r" 'atom-major-m-r-pressed)
  (define-key atom-major-mode-map "\C-c\C-r" 'atom-major-c-r-pressed)
					;some functions
  (defun atom-major-mode-info ()
    (interactive)
    (message (concat "MODE:" (prin1-to-string major-mode))))

  (defun atom-major-m-r-pressed()
    (interactive)
    (message "M-<RET> pressed in atom-major-mode"))
  (defun atom-major-c-r-pressed()
    (interactive)
    (message "ctrl-pressed!!!"))
  
  (use-local-map atom-major-mode-map)	; Select the mode's keymap.
  (run-mode-hooks 'atom-major-mode-hook)) ;telling the mode to run it's hooks


;the derived modes below are basically just
;changing the behaviour of one of the key-chords.
;they inherit behaviours of the major mode,
;and override others when they want to.
;this code could go into a separate file.
(define-derived-mode atom-derived-mode-ONE atom-major-mode
  "atom-derived-mode-ONE"
  (define-key atom-derived-mode-ONE-map "\M-\r" 'atom-derived-mode-one-m-r-pressed)
  (defun atom-derived-mode-one-m-r-pressed()
    (interactive)
    (message "M-<RET> pressed in atom-derived-mode-ONE")))

(define-derived-mode atom-derived-mode-TWO atom-major-mode
  "atom-derived-mode-TWO"
  (define-key atom-derived-mode-TWO-map "\M-\r" 'atom-derived-mode-two-m-r-pressed)
  (defun atom-derived-mode-two-m-r-pressed()
    (interactive)
    (message "M-<RET> pressed in atom-derived-mode-TWO")))

;here is how you would add a lambda function to the
;major-mode-hook (you could put this in another file
;if you wished)
(add-hook 'atom-major-mode-hook
	  (lambda ()
	    (message "Extra function added to atom-major-mode-hook has just run...")))

;defining a minor mode!!!
;remember - minor modes actually run independantly
;of anything else, so you can run them over other modes.

(defcustom atom-minor-mode-hook nil
    "Hook for customizing 'atom-minor-mode'."
    :type 'hook
    :group 'ATOMIC)
  
 (setq atom-minor-mode-map (make-sparse-keymap))
  (define-key atom-minor-mode-map "\M-\r" (lambda () (interactive) (message "M-<RET> pressed in atom-minor-mode")))

(define-minor-mode atom-minor-mode
    "Example minor mode"
    nil " atom (minor)" atom-minor-mode-map
    (run-hooks 'atom-minor-mode-hook))